<template>
  <div>
    <div>
      <br />
      <el-col :span="3">
        <el-input v-model="patientname" placeholder="请输入患者姓名"></el-input>
      </el-col>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<el-button @click="select"
        >查询</el-button
      >
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<el-button @click="findAll"
        >查询全部</el-button
      >
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<el-button @click="findIn"
        >未出院</el-button
      >
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<el-button @click="findOut"
        >已出院</el-button
      >

      <br /><br />
      <el-divider></el-divider>
    </div>

    <el-dialog title="住院信息调整" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="患者姓名" :label-width="formLabelWidth">
          <el-input
            v-model="form.patient.patientName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="所在病房" :label-width="formLabelWidth">
          <div class="block">
            <el-cascader
              v-model="form.sickroomId"
              :options="options"
              :show-all-levels="false"
              :props="{
                value: 'sickroomId',
                label: 'sickroomCode',
                emitPath: false,
                checkStrictly: true,
              }"
              @change="handleChange"
            ></el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="所在部门" :label-width="formLabelWidth">
          <div class="block">
            <el-cascader
              v-model="form.deptId"
              :options="depts"
              :show-all-levels="false"
              :props="{
                value: 'deptId',
                label: 'deptName',
                children: 'children',
                emitPath: false,
                checkStrictly: true,
              }"
              @change="handleChange"
            ></el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="管理医师" :label-width="formLabelWidth">
          <div class="block">
            <el-cascader
              v-model="form.doctor"
              :options="users"
              :show-all-levels="false"
              :props="{
                value: 'userId',
                label: 'realName',
                children: 'children',
                emitPath: false,
                checkStrictly: true,
              }"
              @change="handleChange"
            ></el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="患者状态" :label-width="formLabelWidth">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人名称" :label-width="formLabelWidth">
          <el-input v-model="form.linkName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人电话" :label-width="formLabelWidth">
          <el-input v-model="form.linkPhone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitform">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" style="width: 95%">
      <el-table-column prop="hospitalizedId" label="住院编号" width="90">
      </el-table-column>
      <el-table-column prop="patient.patientName" label="患者姓名" width="120">
      </el-table-column>
      <el-table-column
        prop="sickroom.sickroomCode"
        label="所在病房"
        width="100"
      >
      </el-table-column>
      <el-table-column label="住院时间" width="140">
        <template slot-scope="scope">
          {{ scope.row.beginDate | dateFilter }}
        </template>
      </el-table-column>
      <el-table-column label="出院时间" width="140">
        <template slot-scope="scope">
          {{ scope.row.endDate | dateFilter }}
        </template>
      </el-table-column>
      <el-table-column prop="dept.deptName" label="所在部门" width="120">
      </el-table-column>
      <el-table-column prop="user.realName" label="管理医师" width="100">
      </el-table-column>
      <el-table-column prop="remark" label="患者状态" width="120">
      </el-table-column>
      <el-table-column prop="linkName" label="紧急联系人" width="80">
      </el-table-column>
      <el-table-column label="操作" width="230">
        <template slot-scope="scope">
          <div v-if="scope.row.status < 1">
            <el-button @click="del(scope.row)" size="small">办理出院</el-button>
            <!--             <el-button @click="addchufang(scope.row)" size="small">添加处方</el-button> -->
          </div>
          <el-button @click="update(scope.row)" size="small"
            >修改信息</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getlist,
  selectIn,
  selectOut,
  del,
  update,
  select,
} from "@/api/hospitalized/hospitalized.js";
export default {
  data() {
    return {
      options: [],
      tableData: [],
      currentPage: 1,
      total: 0,
      currentPage: 1,
      pageSize: 5,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      form: {
        patient: {},
      },
      users: [],
      patientname: "",
      selectData: [],
      status: "",
      sickroomId: "",
      depts: [],
    };
  },
  filters: {
    dateFilter(val) {
      let date = new Date(val);
      return (
        date.getFullYear() +
        "/" +
        (date.getMonth() + 1) +
        "/" +
        date.getDate() +
        " " +
        date.getUTCHours() +
        ":" +
        date.getUTCMinutes()
      );
    },
  },
  methods: {
    addchufang(row) {
      let hospitalizedId = row.hospitalizedId;
      sessionStorage.setItem("hospitalizedId", hospitalizedId);
    },
    submitform() {
      update(this.form).then((r) => {
        if (r.code == 200) {
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
          this.getlist();
        }
      });
    },
    handleChange(value) {
      console.log(value);
    },
    findOut() {
      selectOut({ pageNumber: this.currentPage, pageSize: this.pageSize }).then(
        (r) => {
          this.status = "out";
          this.tableData = r.data.list;
          this.total = r.data.total;
        }
      );
    },
    findIn() {
      selectIn({ pageNumber: this.currentPage, pageSize: this.pageSize }).then(
        (r) => {
          this.status = "in";
          this.tableData = r.data.list;
          this.total = r.data.total;
        }
      );
    },
    findAll() {
      this.getlist();
    },
    del(row) {
      if (window.confirm("确认病人办理出院？")) {
        del({ hospitalizedId: row.hospitalizedId }).then((r) => {
          {
            if (r.code == 200) {
              this.getlist()
              this.$message.success("办理出院成功");
            }
          }
        });
      }
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.options = row.sickrooms;
      this.users = row.users;
      this.depts = row.depts;
      this.dialogFormVisible = true;
    },
    getlist() {
      getlist({ pageNumber: this.currentPage, pageSize: this.pageSize }).then(
        (r) => {
          this.status = "all";
          this.tableData = r.data.list;
          this.total = r.data.total;
        }
      );
    },
    select() {
      select({
        patientName: this.patientname,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      }).then((r) => {
        this.tableData = r.data.list;
        this.total = r.data.total;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      if (this.status == "in") {
        this.findIn();
      } else if (this.status == "out") {
        this.findOut();
      } else {
        this.getlist();
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      if (this.status == "in") {
        this.findIn();
      } else if (this.status == "out") {
        this.findOut();
      } else {
        this.getlist();
      }
    },
  },
  mounted() {
    this.getlist();
  },
};
</script>

<style>
</style>